﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Sendmail.Tests
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
       
        private void BtnTest1Click(object sender, RoutedEventArgs e)
        {
            Cursor = Cursors.Wait;
            var reps = (int) slider1.Value;
            try
            {
                for (var i = 0; i < reps; i++)
                {
                    var handler = new TestClient("127.0.0.1", 25, textBox1.Text, textBox2.Text);
                    ThreadPool.QueueUserWorkItem(handler.Run);
                }
            }
            catch(Exception exeption)
            {
                Cursor = Cursors.Arrow;
                MessageBox.Show(exeption.Message);
            }
            Cursor = Cursors.Arrow;
        }

        private delegate void NoArgDelegate();
        public static void Refresh(DependencyObject obj)
        {
            obj.Dispatcher.Invoke(DispatcherPriority.Loaded,(NoArgDelegate)delegate { });
        }

        private void Slider1ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (lblCount != null)
            {
                lblCount.Content = (int)slider1.Value;
            }
        }

        private void Slider2ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (lblCount2 != null)
            {
                lblCount2.Content = (int)slider2.Value;
            }
        }

        private void Button1Click(object sender, RoutedEventArgs e)
        {
            Cursor = Cursors.Wait;
            var reps = (int)slider2.Value;
            try
            {
                var handler = new BrokenClient("127.0.0.1", 25, reps);
                ThreadPool.QueueUserWorkItem(handler.Run);
            }
            catch (Exception exception)
            {
                Cursor = Cursors.Arrow;
                MessageBox.Show(exception.Message);
            }
            Cursor = Cursors.Arrow;
        }
    }
}
